/***
This do-file creates 3 figures that compares re-opened states vs control states' 
consumer spending, employment and small business open and creates a figure 
that shows the share of variance in outcomes explained by reopenings as of May 18. 

It also produces Appendix Table 11 showing robustness to the three-week analysis window
***/

* Set $root 
project figstabs, root
if (r(buildrunning)==0) include "${root}/code/config_interactive.do"

* Set globals
project, uses("${root}/code/set_globals.do")
include "${root}/code/set_globals.do"
local category "State Re-Openings"

* Create required subfolders
cap mkdir "${root}/data/derived/State Re-Openings"
cap mkdir "${root}/results/`category'"
cap mkdir "${root}/results/paper numbers"
cap mkdir "${root}/results/paper numbers/`category'"

********************************************************************************
**# 1. Stacked event study
********************************************************************************

* Set list of outcomes 
local outcomes spend_all emp merchants_all 

* List of reopening dates 
local reopening_dates `=mdy(4, 20, 2020)' `=mdy(4, 24, 2020)' `=mdy(4, 27, 2020)' 

*------------------------------------------------------------------------------*
* Prepare dataset for stacked event study 
*------------------------------------------------------------------------------*

* Loop through re-opening dates 
foreach date in `reopening_dates' {
	
	* Loop through variables 
	foreach outcome_var in `outcomes' { 
		
		* Import data
		project, uses("${root}/data/derived/State Re-Openings/main_dataset updated.dta") 
		use "${root}/data/derived/State Re-Openings/main_dataset updated.dta", clear
			
		gisid statefips date
		
		* Only keep observations that are either treated or a control for that re-opening date x variable
		keep if partial_biz_opened == `date' | control_`date'_`outcome_var' == 1 
		assert (partial_biz_opened == `date') == (control_`date'_`outcome_var' == 0)        // only true after the above line of code
		
		* Collapse to the state x day level
		gcollapse (mean) `outcome_var', by(control_`date'_`outcome_var' statefips date)
		
		* Each event in the stacked event study is given weight proportional to the number of treated states for that event
		gunique statefips if control_`date'_`outcome_var' == 1 
		local num_controls = `r(unique)'
		
		gunique statefips if control_`date'_`outcome_var' == 0 
		local num_treated = `r(unique)'
		
		gen weight_`outcome_var' = `num_treated' / `num_controls' if control_`date'_`outcome_var' == 1 
		replace weight_`outcome_var' = 1 if control_`date'_`outcome_var' == 0
	
		* Create event study variables
		gen eventtime = date - `date'
		drop date 
		gen event = `date'
		gen opener = (control_`date'_`outcome_var' == 0)
		
		tempfile event_`date'_var_`outcome_var'
		save `event_`date'_var_`outcome_var''
	}

	* Create one file per re-opening date 
	use `event_`date'_var_emp', clear 
	merge 1:1 eventtime event statefips using `event_`date'_var_spend_all', nogen
	merge 1:1 eventtime event statefips using `event_`date'_var_merchants_all', nogen

	tempfile event_`date'
	save `event_`date''
}

* Stack event studies
clear 
foreach date in `reopening_dates' {
	append using `event_`date''
} 
	
gisid event statefips eventtime
sort statefips event eventtime  

*------------------------------------------------------------------------------*
* Stacked event study 
*------------------------------------------------------------------------------*

* horizon - Analysis window for event study - either two or three weeks around the reopening
foreach horizon in "w2" "w3" {

	* Loop through outcome vars 
	foreach outcome_var in `outcomes' {

		if "`outcome_var'" == "spend_all" {
			local ylab "-40(10)5, nogrid"	
			local ytitle "Change in Consumer Spending (%), Relative to January 2020"
			local text_pos 1 
			local outcome "Consumer Spending"
			local key "reop_spend"
		}
		
		if "`outcome_var'" == "emp" {
			local ylab "-40(10)5, nogrid"	
			local ytitle "Change in Employment (%), Relative to January 2020"
			local text_pos 10 
			local outcome "Employment"
			local key "reop_emp"
		}

		if "`outcome_var'" == "merchants_all" {
			local ylab "-40(10)5, nogrid"	
			local ytitle "Change in Small Businesses Open (%), Relative to January 2020"
			local text_pos 10 
			local xlab "-60(20)20"
			local outcome "Small Businesses Open"
			local key "reop_merch"
		}
	
		local xlab "-100(20)20"
	
		* Estimate diff-in-diff
		preserve
	
		assert inlist(opener, 0, 1)
		keep if mod(eventtime, 7) == 6 
		
		if "`horizon'" == "w2" {
			keep if inlist(eventtime, -8, -1, 6, 13)
		}
	
		if "`horizon'" == "w3" {
			keep if inlist(eventtime, -15, -8, -1, 6, 13, 20)
		}
	
		gen post = (eventtime > 0) 
		reg `outcome_var' i.post##i.opener [w = weight_`outcome_var'], cluster(statefips)
	
		* Record ATTs for table 
		local beta_`outcome_var'_`horizon' : di %4.2f _b[1.post#1.opener]
		local se_`outcome_var'_`horizon' : di %4.2f _se[1.post#1.opener]
		local n_`outcome_var'_`horizon' = `e(N)'
		
		* Signs for graph 
		if (`beta_`outcome_var'_`horizon'' > 0) local did_sign = "+"
		if (`beta_`outcome_var'_`horizon'' < 0) local did_sign = ""
		
		restore
		
		* Event study figures and scalars for the two-week time horizon only
		if "`horizon'" == "w2" {
			
			* Mean of outcome variable at each relative time point 
			* binscatter, discrete does the same thing as just collapsing by eventtime X ever-treated indicator and doing a scatterplot of means
			binscatter `outcome_var' eventtime [w = weight_`outcome_var'] ///
				if mod(eventtime, 7) == 6 & inrange(eventtime, -85, 21), ///
				discrete ///
				line(connect) ///
				by(opener) ///
				xline(0, lpat(dash) lc(oi2)) ///
				text(0 1 "Opening", place(3) size(medsmall) color(oi2)) ///
				caption("Diff-in-Diff Estimate: `did_sign'`beta_`outcome_var'_`horizon'' p.p. (s.e. = `se_`outcome_var'_`horizon'')") ///
				xtitle("Days Relative to Re-opening") ///
				legend(cols(1) order(1 "Control States" 2 "Opening States") pos(7) ring(0)) ///
				ytitle("") ///
				subtitle(`"`ytitle'"', pos(11) span c(gs4)) ///
				ylab(-30 "-30%" -20 "-20%" -10 "-10%" 0 "0%", nogrid) yscale(range(-35 0)) /// 
				${title_`version'} ///
				colors(oi3 oi2) ///
				name(`outcome_var')
	
			oi_graph_export "${root}/results/State Re-Openings/Re-Opened vs Control States `outcome'", type(${fig_type})
	
			*-------------------------------------------------------------------------------
			* Export output numbers to csv file
			*-------------------------------------------------------------------------------
	
			cap erase "${root}/results/paper numbers/`category'/Re-Opened States vs Control States - `outcome'.yaml"
	
			yamlout using "${root}/results/paper numbers/`category'/Re-Opened States vs Control States - `outcome'.yaml", ///
				key("`key'_did") ///
				comment("Diff in Diff Estimate (p.p.)") ///
				value(`beta_`outcome_var'_`horizon'') fmt(%9.2f)
	
			yamlout using "${root}/results/paper numbers/`category'/Re-Opened States vs Control States - `outcome'.yaml", ///
				key("`key'_se") ///
				comment("SE") ///
				value(`se_`outcome_var'_`horizon'') fmt(%9.2f)
		
			project, creates("${root}/results/paper numbers/`category'/Re-Opened States vs Control States - `outcome'.yaml")
		}
	}
}

* Combine event study plots for the two-week horizon 
graph combine spend_all emp merchants_all, rows(1) xsize(13)
oi_graph_export "${root}/results/State Re-Openings/Re-Opened vs Control States - All Outcomes", type(${fig_type})


********************************************************************************
**# 2. Export ATTs for both two-week and three-week horizons to table 
********************************************************************************

*-------------------------------------------------------------------------------
* Mean decline of outcome variable for comparison
*-------------------------------------------------------------------------------

* Spending
project, uses("${root}/data/web/data/Affinity - State - Daily.csv")
import delimited "${root}/data/web/data/Affinity - State - Daily.csv", asdouble clear

gen date = mdy(month, day, year)
keep if date > mdy(3, 25, 2020) & date <= mdy(4, 14, 2020)

gcollapse (mean) spend_all, by(statefips)

sum spend_all 
local decline_spend_all = round(`r(mean)' * 100, 1)

* Employment
project, uses("${root}/data/web/data/Employment - State - Weekly.csv")
import delimited "${root}/data/web/data/Employment - State - Weekly.csv", clear 

gen date = mdy(month, day_endofweek, year)
keep if date > mdy(3, 27, 2020) & date <= mdy(4, 17, 2020)

gcollapse (mean) emp, by(statefips)

sum emp
local decline_emp = round(`r(mean)' * 100, 1)

* Small Businesses Open
project, uses("${root}/data/web/data/Womply - State - Weekly.csv")
import delimited "${root}/data/web/data/Womply - State - Weekly.csv", clear 

gen date = mdy(month, day_endofweek, year)
keep if date > mdy(3, 22, 2020) & date <= mdy(4, 19, 2020)

gcollapse merchants_all, by(statefips)

sum merchants_all 
local decline_merchants_all = round(`r(mean)' * 100, 1)

*------------------------------------------------------------------------------*
* Regression table 
*------------------------------------------------------------------------------*
clear 
set obs 100

foreach var in `outcomes' {

    gen `var'_twoweeks = ""
	replace `var'_twoweeks = "`beta_`var'_w2'" if _n == 1
	replace `var'_twoweeks = "(" + "`se_`var'_w2'" + ")" if _n == 2
	replace `var'_twoweeks = "`n_`var'_w2'" if _n == 3
	replace `var'_twoweeks = "`decline_`var''" + "%" if _n == 4 
	
	gen `var'_threeweeks = ""
	replace `var'_threeweeks = "`beta_`var'_w3'" if _n == 1
	replace `var'_threeweeks = "(" + "`se_`var'_w3'" + ")" if _n == 2
	replace `var'_threeweeks = "`n_`var'_w3'" if _n == 3
}

* Export
export excel "${root}/results/new_app_table_11.xlsx", sheet(new_app_table_11, replace)
project, creates("${root}/results/new_app_table_11.xlsx")


* Spending impacts estimated for two- and three-week horizons 
cap erase "${root}/results/paper numbers/`category'/Re-Opened States vs Control States - Three Weeks Spending.yaml"

foreach period in "two" "three" {
	
	local did_beta_spend_all_`period'weeks: di spend_all_`period'weeks
	
	yamlout using "${root}/results/paper numbers/`category'/Re-Opened States vs Control States - Three Weeks Spending.yaml", ///
		key("did_beta_spend_all_`period'weeks") ///
		comment("Diff in Diff Estimate (p.p.), Spending, `period' weeks") ///
		value(`did_beta_spend_all_`period'weeks') fmt(%9.2f)
}
		
project, creates("${root}/results/paper numbers/`category'/Re-Opened States vs Control States - Three Weeks Spending.yaml")

********************************************************************************
**# 3. Variance explained by reopenings - two week horizon
********************************************************************************
project, uses("${root}/data/derived/State Re-Openings/main_dataset updated.dta")
use "${root}/data/derived/State Re-Openings/main_dataset updated.dta", clear 

* Loop through outcome variables 
foreach outcome_var in `outcomes' {
	
	*1)
	sum `outcome_var' if date == mdy(5, 18, 2020)
	local v1 = `r(Var)'

	*2)
	replace `outcome_var' = `outcome_var' + `beta_`outcome_var'_w2' if partial_biz_opened > date & !missing(nonessential_biz_closed)                       // add full effect if state closed
	replace `outcome_var' = `outcome_var' + 0.5 * `beta_`outcome_var'_w2' if inrange(partial_biz_opened - date, 0, -7) & !missing(nonessential_biz_closed) // add half effect if state is in first week of reopening

	*3)
	sum `outcome_var' if date == mdy(5, 18, 2020)
	local v2 = `r(Var)'

	local share_explained_`outcome_var' = 1 - (`v2' / `v1')

}

* Add plot of r2
clear
set obs 3
gen ord = _n

gen var = "Businesses Open" if ord == 3
replace var = "Spending" if ord == 1
replace var = "Employment" if ord == 2

gen bar = `share_explained_spend_all' if ord == 1
replace bar = `share_explained_emp' if ord == 2
replace bar = `share_explained_merchants_all' if ord == 3

replace bar = 100 * bar 

graph bar bar, over(var, sort(ord) label(labsize(vlarge))) ///
	ytitle("Share of Variance Explained (%)", size(vlarge)) ///
	${title_`version'} ///
	ylab(0 "0%" 5 "5%" 10 "10%" 15 "15%", nogrid labsize(vlarge)) ///
	xsize(14)
oi_graph_export "${root}/results/State Re-Openings/Variance Explained by Reopenings", type(${fig_type})
